package com.wing.unit.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.common.utils.JsonResult;
import com.wing.unit.dao.SupplierDueSubjectDao;
import com.wing.unit.model.entity.SupplierDueSubject;
import com.wing.unit.model.request.SupplierDueSubjectForm;
import com.wing.unit.model.response.AgentDueSubjectVO;
import com.wing.unit.model.response.NodeDataVO;
import com.wing.unit.model.response.SupplierDueSubjectVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 渠道尽调科目表-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class SupplierDueSubjectServiceImpl extends ServiceImpl<SupplierDueSubjectDao, SupplierDueSubject> implements SupplierDueSubjectService {

    @Override
    public int add(SupplierDueSubjectForm supplierDueSubjectForm) {
        if(supplierDueSubjectForm!=null&&supplierDueSubjectForm.getIsFinal()==null){
            supplierDueSubjectForm.setIsFinal(false);
        }
        //校验

        SupplierDueSubject supplierDueSubject =new SupplierDueSubject();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(SupplierDueSubject.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(supplierDueSubjectForm, supplierDueSubject, copyOptions);

        return baseMapper.insert(supplierDueSubject);
    }

    @Override
    public List<SupplierDueSubjectVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<SupplierDueSubjectVO> pageList4app(Page page, Map<String, Object> params) {
        List<SupplierDueSubjectVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public SupplierDueSubjectVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }

    @Override
    public JsonResult getTreeData() {

        List<SupplierDueSubjectVO> list=baseMapper.list4app(new HashMap<>());
        Map<Long,List<NodeDataVO>> map=new HashMap<>();
        for (SupplierDueSubjectVO supplierDueSubjectVO:list) {
            if (map.containsKey(supplierDueSubjectVO.getParentId())){
                NodeDataVO nodeDataVO=new NodeDataVO();
                nodeDataVO.setValue(supplierDueSubjectVO.getId().toString());
                nodeDataVO.setName(supplierDueSubjectVO.getName());
                map.get(supplierDueSubjectVO.getParentId()).add(nodeDataVO);
            }else{
                List<NodeDataVO> tmpList=new ArrayList<>();
                NodeDataVO nodeDataVO=new NodeDataVO();
                nodeDataVO.setValue(supplierDueSubjectVO.getId().toString());
                nodeDataVO.setName(supplierDueSubjectVO.getName());
                tmpList.add(nodeDataVO);
                map.put(supplierDueSubjectVO.getParentId(),tmpList);
            }
        }
        List<NodeDataVO> rootList=map.get(-1L);
        if (rootList==null){
            return JsonResult.success();
        }
        constructTree(rootList,map);
        return JsonResult.success(rootList);
    }

    private void  constructTree(List<NodeDataVO> parentList,Map<Long,List<NodeDataVO>> dataMap){
        for (NodeDataVO node:parentList) {
            Long nodeId=Long.valueOf(node.getValue());
            List<NodeDataVO> childList=dataMap.get(nodeId);
            if (childList!=null){
                node.setChildren(childList);
                constructTree(childList,dataMap);
            }
        }
    }
}
